/**
 * 
 */
package org.jackysoft.controller;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.jackysoft.controller.base.AbstractController;
import org.jackysoft.entity.HtmlFormElement;
import org.jackysoft.entity.HtmlFormTemplate;
import org.jackysoft.entity.OsTask;
import org.jackysoft.entity.OsWorkflowRunner;
import org.jackysoft.entity.StepNode;
import org.jackysoft.service.FieldAccessService;
import org.jackysoft.service.FormService;
import org.jackysoft.service.HtmlFormElementService;
import org.jackysoft.service.HtmlTemplateService;
import org.jackysoft.service.OsWorkflowRunnerService;
import org.jackysoft.workflow.ActionItem;
import org.jackysoft.workflow.Workflow;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.google.common.collect.Maps;

/**
 * 
 * <dd>根据工作流所在 的步骤,工作流名称,步骤编号, <dd>显示html页面,,同时根据对个步骤的字段控制 <dd>生成js控制各自段的启用禁用状态
 * <dd>为了解耦业务和流程，表单采用运行时绑定
 * 
 * @author qujiakang(QQ285799123)
 */

@Controller
@RequestMapping("/generate/html")
public class GenerateHtmlController extends AbstractController<OsTask> {

	@Resource
	private FormService formService;
	@Resource
	private HtmlFormElementService htmldocService;
	@Resource
	private HtmlTemplateService tempService;

	@Resource
	private HtmlFormElementService elementService;
	
	@Resource
	private FieldAccessService fieldService;
		
	@Resource
	Workflow workflow;

	@Resource
	OsWorkflowRunnerService workflowService;
	
	@RequestMapping("/fill/{refid}/{stepId}/{actionId}/{showOnly}")
	public void fill(
			@ModelAttribute Collection<HtmlFormTemplate> templates,
			@ModelAttribute ActionItem actionItem,
			@ModelAttribute String htmlCode,			
			@PathVariable("refid")@ModelAttribute long refid,
			@PathVariable("actionId")@ModelAttribute int actionId,
			@PathVariable("stepId")@ModelAttribute int stepId,
			@PathVariable("showOnly") boolean showOnly
			) throws Exception {

		final OsWorkflowRunner entry =  (OsWorkflowRunner) workflowService.findRunner(refid);
		if (!elementService.hasElement(refid)) {
			templates = tempService.findAll();
			return;
		}
		actionItem = workflow.getActionItemById(actionId,refid,Collections.EMPTY_MAP);
		stepId = actionItem.getStepId();
		Map<Integer, String> nextSteps = Maps.newHashMap();
		for (StepNode step : actionItem.getNextSteps()) {
			nextSteps.put(step.getId(), step.getName());
		}
		htmlCode = formService.generateHtmlForm(
				actionId, 
				stepId, 
				refid,
				nextSteps, 
				entry.getFormTemplate(), 
				showOnly
				);
		
	}

	
	@RequestMapping("/opentemplate/{refid}/{stepId}/{actionId}/{tempId}")
	public void opentemplate(
			@ModelAttribute ActionItem actionItem,
			@ModelAttribute String htmlCode,	
			@PathVariable("tempId") String tempId,
			@PathVariable("refid")@ModelAttribute long refid,
			@PathVariable("actionId")@ModelAttribute int actionId,
			@PathVariable("stepId")@ModelAttribute int stepId
			) throws Exception {
		actionItem = workflow.getActionItemById(actionId, refid,
				Collections.EMPTY_MAP);
		Map<Integer, String> nextSteps = Maps.newHashMap();
		for (StepNode step : actionItem.getNextSteps()) {
			nextSteps.put(step.getId(), step.getName());
		}		
		htmlCode = formService.generateHtmlForm(actionId, stepId, refid,
				nextSteps, tempId, false);
		
	}

	@RequestMapping("/saveform/{refid}/{tempId}/{withForm}")
	public void saveform(
			long refid,
			@PathVariable("tempId") String tempId,
			@PathVariable("withForm") boolean withForm,
			List<HtmlFormElement> formElements
			
			) throws Exception {
		htmldocService.saveFormElement(refid, withForm, tempId,formElements);		
	}

	
	@RequestMapping("/viewform/{refid}/{tempId}/{withForm}")
	public void viewform(
			long refid,
			String tempId,
			@ModelAttribute String htmlCode
			) throws Exception {
				
		if (!elementService.hasElement(refid)) {
			htmlCode = "此工作流还没有创建表单模版";
			return ;
		}
		
		htmlCode = formService.generateHtmlForm(refid,tempId);		
	}	
}